package com.shadow.common.bo;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Collection;
import java.util.HashMap;

/**
 * <p>
 *  系统统一返回
 * </p>
 *
 * @author shadow
 * @since 2022-09-24
 */
public class R extends HashMap<String, Object> {

    public static final int SUCCESS_CODE = 1;     // 正常
    public static final int FAIL_CODE    = 0;     // 失败
    public static final String CODE   = "code";   //1正常、0失败
    public static final String STATUS = "status"; //200成功、RCodeEnums系统自定义错误状态码
    public static final String MSG    = "msg";
    public static final String DATA    = "data";
    public static final String LIST   = "list";
    public static final String PAGE   = "page";
    public static final String MAP    = "map";
    public static final String COUNT = "count";

    public static final String SUCCESS_MSG = "success";
    public static final String FAIL_MSG = "fail";

    public R() {
    }

    /**
     * 成功
     **/
    public static R success() {
        R r = new R();
        r.put(CODE, SUCCESS_CODE);
        r.put(MSG, SUCCESS_MSG);
        r.put(DATA, SUCCESS_MSG);
        return r;
    }

    /**
     * 成功 - msg
     **/
    public static R success(final String msg) {
        R r = new R();
        r.put(CODE, SUCCESS_CODE);
        r.put(DATA, SUCCESS_MSG);
        r.put(MSG, msg);
        return r;
    }

    /**
     * 成功 - msg & data
     **/
    public static R success(final String msg, final Object data) {
        R r = new R();
        r.put(CODE, SUCCESS_CODE);
        r.put(DATA, data);
        r.put(MSG, msg);
        return r;
    }

    /**
     * 失败
     **/
    public static R fail() {
        R r = new R();
        r.put(CODE, FAIL_CODE);
        r.put(MSG, FAIL_MSG);
        r.put(DATA, FAIL_MSG);
        return r;
    }

    /**
     * 失败 - msg
     **/
    public static R fail(final String msg) {
        R r = new R();
        r.put(CODE, FAIL_CODE);
        r.put(MSG, msg);
        r.put(DATA, FAIL_MSG);
        return r;
    }

    /**
     * LayUi分页接口通用构造
     *
     * @param data 数据列表
     * @param count 总数
     * @return {@link R}
     **/
    public static R successForLayUi(final Collection<?> data, final int count) {
        final R r = new R();
        r.put(R.CODE, FAIL_CODE); // LayUi 中0成功, 1失败
        r.put(R.DATA, data);
        r.put(R.COUNT, count);
        r.put(R.MSG, SUCCESS_MSG);
        return r;
    }

    /**
     * LayUi分页接口通用构造 ==> IPage
     *
     * @param iPage mp分页对象
     * @return {@link R}
     **/
    public static R successForLayUi(final IPage<?> iPage) {
        final R r = new R();
        r.put(R.CODE, FAIL_CODE); // LayUi 中0成功, 1失败
        r.put(R.DATA, iPage.getRecords());
        r.put(R.COUNT, iPage.getTotal());
        r.put(R.MSG, SUCCESS_MSG);
        return r;
    }

    /**
     * 重写put方法, 更改返回对象
     *
     * @param key 键
     * @param value 值
     * @return {@link R}
     * */
    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
